package com.qf.hospital.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.hospital.VO.R;
import com.qf.hospital.enums.HospitalExceptionEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/15 20:31
 */
@RestController
public class LoginController {

    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @PostMapping("/sel/doLogin")
    public R doLogin(String loginname , String pwd){
        //校验请求参数
        if (StringUtils.isEmpty(loginname)||StringUtils.isEmpty(pwd)){
            return R.error(HospitalExceptionEnum.USERNAME_PASSWORD_ERROR,"用户名和密码均为必填");
        }
        //获取主体
        Subject subject = SecurityUtils.getSubject();

        //执行认证
        subject.login(new UsernamePasswordToken(loginname, pwd));

        //登录成功
        return R.ok("登录成功");
    }

    @GetMapping ("/sel/logout")
    public R logout(){
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return R.ok("退出登录成功");
    }
}
